package com.yb.project.algorithm.practice.test.test;

import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

public class Main {
    /**
     * 5
     * 1 2 2 1 2 3 4
     * 1 1 1 1 1 1 1
     * @param args
     */
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
//        String str1 = in.nextLine();
//        String str2 = in.nextLine();
//        String str3 = in.nextLine();
        String str1 = "5";
        String str2 = "1 2 2 1 2 3 4";
        String str3 = "1 1 1 1 1 1 1";
        String[] arr1 = str2.split(" ");
        String[] arr2 = str3.split(" ");
        // cache cost
        int m = Integer.valueOf(str1);
        // file num
        Map<String,Long> map1 =new HashMap<>();
        // file size
        Map<String,Long> map2 =new HashMap<>();
        for(int i=0;i<arr1.length;i++){
            map1.put(arr1[i],map1.getOrDefault(arr1[i],(long)0)+1);
            map2.put(arr1[i],Long.valueOf(arr2[i]));
        }
        long res = 0;
        for(String str: map1.keySet()){
            long num = map1.get(str);
            long size = map2.get(str);
            if(num*size<(size+m)){
                res+=num*size;
            }else{
                res+=(size+m);
            }
        }
        System.out.println(res);
    }
}
